package com.tms.app.tms.ui.feeinfo;

import android.content.Intent;
import android.view.View;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tms.app.tms.R;
import com.tms.app.tms.adapter.FeeInfoAdapter;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.bean.Fee;
import com.tms.app.tms.bean.UserInfo;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.ExFeeInfo;
import com.tms.app.tms.bean.response.FeeInfoRes;
import com.tms.app.tms.bean.response.UserInfoRes;
import com.tms.app.tms.ui.FeeAddAc;
import com.tms.app.tms.ui.base.presenter.AbsServicexUtils;
import com.tms.app.tms.ui.base.presenter.BaseListPresenter;
import com.tms.app.tms.widget.CommonDialog;

import org.xutils.http.RequestParams;

import java.io.File;

/**
 * Created by Administrator on 2017/11/27 0027.
 */

public class FeeInfoPresenter extends BaseListPresenter {
    private int waybillExId;
    private int planId;
    private int planIdShowType;
    private CommonDialog commonDialog;

    public void setPlanIdShowType(int planIdShowType) {
        this.planIdShowType = planIdShowType;
    }
    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        if (requestType==1){//费用信息
            FeeInfoRes userInfoRes=gson.fromJson(result, FeeInfoRes.class);
            bindDataToView(userInfoRes);
            return;
        }
        if (requestType==2){//异常费用信息
            ExFeeInfo feeInfo=gson.fromJson(result,ExFeeInfo.class);
            FeeInfoRes userInfoRes=gson.fromJson(result, FeeInfoRes.class);
            bindDataToView(userInfoRes);
            return;
        }
        if (requestType==3){//删除异常费用

        }

    }

    private void bindDataToView(FeeInfoRes userInfoRes) {
        if (userInfoRes.getCode()==200&&userInfoRes.getData()!=null&&userInfoRes.getData().size()>0){
            FeeInfoAdapter adapter=iView.getAdapter(0);
            adapter.getData().clear();
            adapter.getData().addAll(userInfoRes.getData());
            if (requestType==1){
                for (Fee fee : adapter.getData()) {
                    fee.setShowType(0);
                    fee.setPlanIdShowType(planIdShowType);
                }
            }else {
                for (Fee fee : adapter.getData()) {
                    fee.setShowType(1);
                    fee.setPlanIdShowType(planIdShowType);
                }
            }
            adapter.notifyDataSetChanged();
            iView.refreshComplite();
            return;
        }
        iView.refreshNoData();
        if (userInfoRes.getCode()!=200)
            iView.showMsg(userInfoRes.getError());
    }

    /*****
     * 费用信息
     * @param token
     * @param planId
     */
    public void getData(String token ,int planId){// 费用信息
        this.token=token;
        this.planId=planId;
        requestType=1;
        RequestParams params=new RequestParams(Constant.Fee_detail);
        params.addBodyParameter("token",token);
        params.addBodyParameter("planId",planId+"");
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params);
    }

    /****
     * 异常费用信息
     * @param token
     * @param waybillExecId
     */
    public void getExceptionFeeList(String token ,int waybillExecId){//
        this.token=token;
        this.waybillExId=waybillExecId;
        requestType=2;
        RequestParams params=new RequestParams(Constant.ExFeeList);
        params.addBodyParameter("token",token);
        params.addBodyParameter("waybillExecId",waybillExecId+"");
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params);
    }


    @Override
    public void onRefresh() {
        super.onRefresh();
        if (requestType==1){
            getData(token,planId);
            return;
        }
        if (requestType==2){
            getExceptionFeeList(token,waybillExId);
            return;
        }
    }

    /******
     * 删除异常费用
     * @param token
     * @param feeId
     */
    public void delFee(String token,int feeId){
        requestType=3;
        this.token=token;
        RequestParams params=new RequestParams(Constant.FeeDel);
        params.addBodyParameter("token",token);
        params.addBodyParameter("feeId",feeId+"");
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params);
    }

    @Override
    public void onItemChildClick(final BaseQuickAdapter adapter, View view, final int position) {
        super.onItemChildClick(adapter, view, position);
        final Fee fee= (Fee) adapter.getData().get(position);
        if (view.getId()== R.id.text_del){

            commonDialog=CommonDialog.getInstance(view.getContext(), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delFee(token,fee.getFeeId());
                    commonDialog.close();
                    adapter.getData().remove(position);
                    adapter.notifyDataSetChanged();
                }
            },"确定要删除费用吗");
            commonDialog.show();

        }else {
            Intent intent=new Intent(view.getContext(), FeeAddAc.class);
            intent.putExtra("fee",gson.toJson(fee));
            intent.putExtra("waybillNo",fee.getWaybillNo());
            intent.putExtra("waybillExecId",waybillExId);
            intent.putExtra("planIdShowType",planIdShowType);
            iView.startActivityForResult(intent,1);
        }

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
    }
}
